﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using appDataContracts;
using System.Data;

namespace appSupplier.UserControls
{
    /// <summary>
    /// Logique d'interaction pour UCWelcome.xaml
    /// </summary>
    public partial class UCCatalogs : UserControl
    {

        public UCCatalogs()
        {
            InitializeComponent();
            dgCatalog.ItemsSource = (List<SerializedCatalog>)AppWindow.supplierService.getSupplierCatalogs(AppWindow.supplier.SUPPLIERID);
        }

        private void btCreateCatalog_Click(object sender, RoutedEventArgs e)
        {
            new CreateCatalogWindow(this, false).ShowDialog();
        }

        public void update()
        {
            dgCatalog.ItemsSource = (List<SerializedCatalog>)AppWindow.supplierService.getSupplierCatalogs(AppWindow.supplier.SUPPLIERID);
        }

        private void btEdit_Click(object sender, RoutedEventArgs e)
        {
            new CreateCatalogWindow(this, true).ShowDialog();
        }

        private void btDelete_Click(object sender, RoutedEventArgs e)
        {
            AppWindow.supplierService.deleteCatalog(((SerializedCatalog)dgCatalog.SelectedItem).CATALOGID);
            update();
        }

        private void dgCatalog_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dgCatalog.SelectedItems.Count > 0)
            {
                btEdit.Visibility = Visibility.Visible;
                btDelete.Visibility = Visibility.Visible;
                btShowProducts.Visibility = Visibility.Visible;
            }
            else
            {
                btEdit.Visibility = Visibility.Hidden;
                btDelete.Visibility = Visibility.Hidden;
                btShowProducts.Visibility = Visibility.Hidden;
            }
        }

        //public void hideButtons()
        //{
        //    if (dgCatalog.SelectedItems.Count == 0)
        //    {
        //        btEdit.Visibility = Visibility.Hidden;
        //        btDelete.Visibility = Visibility.Hidden;
        //        btShowProducts.Visibility = Visibility.Hidden;
        //    }
        //}

        private void btShowProducts_Click(object sender, RoutedEventArgs e)
        {
            int selectedId = ((SerializedCatalog)dgCatalog.SelectedItem).CATALOGID;
            string selectedName = ((SerializedCatalog)dgCatalog.SelectedItem).CATALOGNAME;
            new ShowProductsWindow(selectedId, selectedName).ShowDialog();
        }

        private void dgCatalog_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (dgCatalog.SelectedItems.Count > 0)
            {
                btEdit_Click(sender, e);
            }
        }


    }

}
